home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-06-23 | 7.2 KB | 280 lines |
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Hashtable;
-
- /**
- * An abstract class to implement the core features of a component with
- * button like behavior, and the capability to display several images
- * representing different states of the button.
- */
- public abstract class ImageButton extends Component
- {
- //Declare data members
- //Insert "ImageButton data members"
- protected Hashtable imageHash;
- protected Image image;
- protected String imageName;
- protected boolean isMouseDown = false;
- protected boolean isMouseInside = false;
- protected String actionCommand;
- protected ActionListener actionListener = null;
-
- /**
- * Constructs a default instance of this class.
- */
- public ImageButton()
- {
- //REGISTER_LISTENERS
- //Insert "ImageButton register listeners"
- Mouse aMouse = new Mouse();
- this.addMouseListener(aMouse);
-
- //Initialize state information
- //Insert "ImageButton init state"
- imageHash = new Hashtable();
- actionCommand = "ImageButton Action";
- }
-
- /**
- * Gets called when the mouse button is released on this button.
- * @param isMouseInside, if true, the mouse is located inside the button area,
- * if false the mouse is outside the button area.
- */
- protected void handleMouseRelease(boolean isMouseInside)
- {
- //Handle firing an ActionEvent to our listeners if the mouse was released
- //inside the button.
- //Insert "ImageButton handleMouseReleased"
- if (isMouseInside)
- fireActionEvent();
- }
-
- /**
- * Gets called when the mouse crosses into or out of the button area.
- * @param isMouseInside, is true if the mouse is in the button area,
- * false if it is outside.
- * @param isMouseDown, is true if the mouse button is pressed, false if it is not.
- */
- protected abstract void handleRollover(boolean isMouseInside, boolean isMouseDown);
-
- /**
- * Gets called when the mouse button is pressed on this button.
- */
- protected abstract void handleMousePressed();
-
-
- /**
- * Adds an image to the button.
- * @param imagePath, the location of the image resource to use.
- * This path is relative to the location of this class file.
- * @param imageName, the name used to identify the image for later
- * use in this button.
- * @see #removeImage
- */
- public void addImage(String imagePath, String imageName)
- {
- //Handle storing the information in our internal data structure.
- //Insert "ImageButton addImage"
- if (imageName != null && !imageName.equals(""))
- {
- Image newImage = Misc.loadImage(imagePath, this, true);
- if (newImage != null)
- {
- imageHash.put(imageName, newImage);
- }
- }
- }
-
- /**
- * Removes an image from the button
- * @param imageName, the identifying name of the image to remove.
- * @see #addImage
- */
- public void removeImage(String imageName)
- {
- //Handle removing the image from our internal data structure.
- //Insert "ImageButton removeImage"
- if (imageName != null && !imageName.equals(""))
- {
- imageHash.remove(imageName);
- }
- }
-
- /**
- * Sets the image for the button to use as its current image.
- * @param imageName, the identifying name of the image to use.
- */
- public void setImage(String imageName)
- {
- //Handle locating the image in our internal data structure,
- //setting it as the current image, and repainting the button.
- //Insert "ImageButton setImage"
- if (imageName != null && !imageName.equals(""))
- {
- Image temp = (Image)imageHash.get(imageName);
- if (temp != null)
- {
- image = temp;
- this.imageName = imageName;
- repaint();
- }
- }
- }
-
- /**
- * Gets the name of the image currently in use.
- * @return The identifying name of the image being used.
- */
- public String getImage()
- {
- //Return the current image name.
- //Insert "ImageButton getImage"
- return imageName;
- }
-
- /**
- * Gets the actual Image Object which is currently being used.
- * @return The java.awt.Image currently in use.
- */
- public Image getImageObject()
- {
- //Return the current image object.
- //Insert "ImageButton getImageObject"
- return image;
- }
-
-
- //Routines for handling ActionListener management.
- //Insert "ImageButton Action Management"
- /**
- * Sets the command name of the action event fired by this button.
- * @param command The name of the action event command fired by this button
- */
- public void setActionCommand(String command)
- {
- actionCommand = command;
- }
-
- /**
- * Returns the command name of the action event fired by this button.
- * @return the action command name
- */
- public String getActionCommand()
- {
- return actionCommand;
- }
-
- /**
- * Adds the specified action listener to receive action events
- * from this button.
- * @param l the action listener
- */
- public void addActionListener(ActionListener l)
- {
- actionListener = AWTEventMulticaster.add(actionListener, l);
- }
-
- /**
- * Removes the specified action listener so it no longer receives
- * action events from this button.
- * @param l the action listener
- */
- public void removeActionListener(ActionListener l)
- {
- actionListener = AWTEventMulticaster.remove(actionListener, l);
- }
-
- /**
- * Fire an action event to the listeners.
- */
- protected void fireActionEvent()
- {
- if (actionListener != null)
- actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
- }
-
- /**
- * Returns the preferred size of this component.
- * @see #getMinimumSize
- * @see LayoutManager
- */
- public Dimension getPreferredSize()
- {
- //If the current image is not null, then return the size of the image.
- //If it is null, defer to the super class.
- //Insert "ImageButton getPreferredSize"
- if (image != null)
- return new Dimension(image.getWidth(this), image.getHeight(this));
-
- return super.getPreferredSize();
- }
-
- /**
- * Paints the component. This method is called when the contents
- * of the component should be painted in response to the component
- * first being shown or damage needing repair. The clip rectangle
- * in the Graphics parameter will be set to the area which needs
- * to be painted.
- * @param g the specified Graphics window
- * @see #update
- */
- public void paint(Graphics g)
- {
- //Let the super class draw, then handle drawing the current image.
- //Insert "ImageButton paint"
- super.paint(g);
-
- if (image != null)
- g.drawImage(image, 0, 0, this);
- }
-
- //Inner class for handing mouse events.
- //Insert "ImageButton Mouse Handling"
- class Mouse extends MouseAdapter
- {
- public void mouseExited(MouseEvent event)
- {
- ImageButton_MouseExited(event);
- }
-
- public void mouseEntered(MouseEvent event)
- {
- ImageButton_MouseEntered(event);
- }
-
- public void mouseReleased(MouseEvent event)
- {
- ImageButton_MouseReleased(event);
- }
-
- public void mousePressed(MouseEvent event)
- {
- ImageButton_MousePressed(event);
- }
- }
-
- protected void ImageButton_MousePressed(MouseEvent event)
- {
- isMouseDown = true;
- handleMousePressed();
- }
-
- protected void ImageButton_MouseReleased(MouseEvent event)
- {
- isMouseDown = false;
- handleMouseRelease(isMouseInside);
- }
-
- protected void ImageButton_MouseEntered(MouseEvent event)
- {
- isMouseInside = true;
- handleRollover(isMouseInside, isMouseDown);
- }
-
- protected void ImageButton_MouseExited(MouseEvent event)
- {
- isMouseInside = false;
- handleRollover(isMouseInside, isMouseDown);
- }
- }
-